import * as THREE from "three";
import { Entity } from "../entity/Entity";
import { Random } from "../util/Random";
import { GameWorld } from "../world/World";
import { SystemBase } from "./SystemBase";

//增删物体和场景初始化

export class EntitySystem extends SystemBase {
    constructor(world: GameWorld) {
        super(world);
    }

    AddEntity(e: Entity) {
        this.world.entityList.DelayAdd(e);
        this.world.gameObjectSystem.Add(e.gameObject, e.position, e.size);
    }

    RemoveEntity(e: Entity) {
        this.world.entityList.DelayRemove(e);
        if (e.gameObject != null)
            this.world.gameObjectSystem.Remove(e.gameObject);
    }

    AddRandomEntity() {
        let e: Entity = new Entity();
        e.size.value = 0.15;
        // e.team.id = 0;
        const geo = this.world.box.geometry;
        geo.computeBoundingBox();
        e.position.value = new THREE.Vector3(
            Random.range(geo.boundingBox.min.x + e.size.value, geo.boundingBox.max.x - e.size.value),
            Random.range(geo.boundingBox.min.y + e.size.value, geo.boundingBox.max.y - e.size.value),
            1
        );
        this.AddEntity(e);
    }

    AddMoveAbleEnity(e: Entity) {
        this.AddEntity(e);
        // console.log(e.gameObject.mesh.uuid);
        this.world.playerEntityList.DelayAdd(e);

        this.world.moveSystem.Add(e.position);
    }

    InitScene() {
        for (let i = 0; i < 50; i++) {
            this.AddRandomEntity();
        }

        for (let i = 0; i < 1; i++) {
            let playerEntity: Entity = new Entity();
            playerEntity.position.value = new THREE.Vector3(0, 0, 1);
            playerEntity.size.value = 0.3;
            // playerEntity.team.id = 1;
            playerEntity.position.value = new THREE.Vector3(Random.range(-0.1, 0.1), Random.range(-0.1, 0.1));
            this.AddMoveAbleEnity(playerEntity);
        }
    }
}
